/*****************************************************************************
** CS221 Computer Vision Project
** Copyright (c) 2006, Stanford University
**
** FILENAME:    KMeans.h
** AUTHOR(S):  	Chanh Nguyen <chanhn@stanford.edu>
** 				Divij Gupta <divijg@stanford.edu>
** 				Yongwhan Lim <yongwhan@stanford.edu>
** DESCRIPTION:
** This runs the KMeans algorithm over an image to find the K clusters.
**

*****************************************************************************/

#pragma once

#include "cv.h"
#include "cxcore.h"

#include "utils.h"
#include "objects.h"
#include "template.h"

typedef struct KPoint {
	int x;
	int y;
	float values[3];
};

typedef struct KCluster {
	float center[3];
	float prev_center[3];
	std::vector<KPoint> points;
	float random[3];
} KCluster;


/* Classifier class ---------------------------------------------------------
 */
class KMeans {
protected:

    int num_clusters;
    std::vector<KCluster > clusters;
    const IplImage *image;
	bool converged;

public:
    // constructors
    KMeans(const IplImage *image, int num_clusters);

    // destructor
    virtual ~KMeans();

    // load and save classifier configuration
    virtual std::vector<KCluster> findClusters();
    virtual IplImage* getClusterImage();
    virtual IplImage* getSingleClusterImage(int cluster_num);

private:

    virtual void findAverage(KCluster *c);
    virtual void findFarthest(KCluster *c);
    virtual float distance(float a[], float b[]);
    virtual int assignCluster(KPoint &p);

};




